<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of user
 *
 * @author Admin
 */
class user extends MY_Controller {

	public function index() {
		$this->data['temp'] = 'admin/user/index';
		$this->load->view('admin/main', $this->data);
	}

	public function create() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_rules('username', 'lang:user_name', 'required|min_length[3]|max_length[20]');
		$this->form_validation->set_rules('email', 'lang:user_email', 'required|valid_email');
		$this->form_validation->set_rules('password', 'lang:user_password', 'required|min_length[3]|max_length[20]');
		$this->form_validation->set_rules('confirm', 'lang:user_password_confirm', 'required|matches[password]');
		$data = request_filter(array('username', 'password', 'email', 'group_id', 'fullname'));
		if ($this->form_validation->run()) {
			$data['password'] = md5($data['password']);
			unset($data['confirm']);
			$this->model_user->create($data);
			echo json_encode(array(
				'title' => lang('notice'),
				'state' => 'success',
				'message' => lang('create_success')
			));
		} else {
			$error = form_error_message($data);
			echo json_encode(array(
				'title' => lang('error'),
				'state' => 'error',
				'message' => $error,
			));
		}
	}

	public function read() {
		$users_count = $this->model_user->get_total();
		$page = request('p', 1);
		$pagination = pagination_init($users_count, $page);
		$users = $this->model_user->get_list(array(
			'limit' => array($pagination->from, $pagination->limit)
				));
		$this->load->model('model_group');
		foreach ($users as &$user) {
			$group = $this->model_group->get_info($user->group_id);
			if ($group)
				$user->group = $group->name;
		}
		$result = array(
			'title' => lang('notice'),
			'state' => 'success',
			'data' => $users,
			'pagination' => $pagination,
		);
		echo json_encode($result);
	}

	public function detail() {
		$id = request('id');
		$user = $this->model_user->get_info($id);
		$result = array(
			'title' => lang('notice'),
			'state' => 'success',
			'data' => $user
		);
		//pr($module);die();
		echo json_encode($result);
	}

	public function update() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$id = request('id');
		if (!$this->isRootAdmin() && in_array($id, $this->config->item('root_admin'))) {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('action_rejected'),
			));
			exit();
		}
		$password = request('password');

		$this->form_validation->set_rules('username', 'lang:user_name', 'required|min_length[3]|max_length[20]');
		$this->form_validation->set_rules('email', 'lang:user_email', 'required|valid_email');

		// Neu cap nhat password thi kiem tra trung`
		if (strlen($password) > 0) {
			$this->form_validation->set_rules('password', 'lang:user_password', 'required|min_length[3]|max_length[20]');
			$this->form_validation->set_rules('confirm', 'lang:user_password_confirm', 'required|matches[password]');
		}

		$data = request_filter(array('username', 'email', 'group_id', 'fullname', 'confirm'));

		if ($this->form_validation->run()) {

			// Cap nhat password
			if (strlen($password) > 0)
				$data['password'] = md5($password);
			unset($data['confirm']);
			$this->model_user->update($id, $data);
			echo json_encode(array(
				'id' => $id,
				'title' => lang('notice'),
				'state' => 'success',
				'message' => lang('user_update_success')
			));
		}
		else {
			echo json_encode(array(
				'title' => lang('error'),
				'state' => 'error',
				'message' => form_error_message($data),
			));
		}
	}

	public function delete() {
		$id = request('id');
		if (!$this->isRootAdmin() && in_array($id, $this->config->item('root_admin'))) {
			echo json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('action_rejected'),
			));
		} else {
			$this->model_user->del($id);
			$result = array(
				'id' => $id,
				'title' => lang('notice'),
				'state' => 'success',
				'message' => lang('module_delete_success')
			);
			echo json_encode($result);
		}
	}

}

?>
